package br.brpv.silvestrin.conferencia.dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import br.brpv.silvestrin.conferencia.R;

public class AddLoteDialog extends DialogFragment {
	
	// Use this instance of the interface to deliver action events
	private AddLoteDialogListener mListener;
	private EditText loteNumberEditText;
    
    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (AddLoteDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement AddLoteDialogListener");
        }
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_add_lote, null);
        
        builder.setTitle(R.string.dialog_add_lote_title);
        
        builder.setView(dialogView);
        
        builder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       mListener.onDialogAddLoteClick(loteNumberEditText.getText().toString());
                   }
               });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       dismiss();
                   }
               });
        
        loteNumberEditText = (EditText)dialogView.findViewById(R.id.edit_lote_origin_lote_number_editText); 
        // Create the AlertDialog object and return it
        return builder.create();
    }

    
    public interface AddLoteDialogListener {
        public void onDialogAddLoteClick(String loteNumber);
    }
}
